#!/usr/bin/env python
import sys
from crewaiwithqwen.crew import CrewaiwithqwenCrew
import crewaiwithqwen.crew as crewaiWhichWeUse

import os
os.environ["OPENAI_API_BASE"] = 'http://localhost:11434/v1' 
os.environ["OPENAI_API_KEY"] ='NA'

from langchain_openai import ChatOpenAI
llm = ChatOpenAI(model="ollama/qwen2.5:7b")

crew_instance = crewaiWhichWeUse.CrewaiToCHOOSEQustions()

# This main file is intended to be a way for you to run your
# crew locally, so refrain from adding unnecessary logic into this file.
# Replace with inputs you want to test with, it will automatically
# interpolate any tasks and agents information

# 这个主文件的目的是让你在本地运行你的船员，所以不要在这个文件中添加不必要的逻辑。
# 替换为你想要测试的输入，它会自动插入任何任务和代理信息


def run():
    """
    Run the crew.
    """
    inputs = {
        'chapter': '第一、二、三章',
        'knowledge_points': [],
        'user_demand': '生成高质量的选择题'
    }
    crew_instance.crew().kickoff(inputs=inputs)
    
def train():
    """
    Train the crew for a given number of iterations.
    """
    inputs = {
        'chapter': '第九章'
    }
    try:
        result=crew_instance.crew().train(n_iterations=int(sys.argv[1]), filename=sys.argv[2], inputs=inputs)
        print(result)

    except Exception as e:
        raise Exception(f"An error occurred while training the crew: {e}")

def replay():
    """
    Replay the crew execution from a specific task.
    """
    try:
        crew_instance.crew().replay(task_id=sys.argv[1])

    except Exception as e:
        raise Exception(f"An error occurred while replaying the crew: {e}")

def test():
    """
    Test the crew execution and returns the results.
    """
    inputs = {
        "topic": "AI LLMs"
    }
    try:
        crew_instance.crew().test(n_iterations=int(sys.argv[1]), openai_model_name=sys.argv[2], inputs=inputs)

    except Exception as e:
        raise Exception(f"An error occurred while replaying the crew: {e}")

if __name__ == "__main__":
    run()